package com.cheer.controller;

import com.cheer.dto.AdminSaveDTO;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.AdminService;
import com.cheer.vo.AdminVO;
import com.cheer.vo.Page;
import com.cheer.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    /**
     * 分页查询管理员用户
     */
    @GetMapping("/page")
    public Result<Page<AdminVO>> page(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                      @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                      @RequestParam(required = false) Long orgId,
                                      @RequestParam(required = false) String orgName,
                                      @RequestParam(required = false) String contactPerson) {
        if (orgId == null) {
            orgId = userInfoHolder.getAdminOrgId();
        }
        Page<AdminVO> page = adminService.getPage(pageNum, pageSize, orgId, orgName, contactPerson);
        return Result.success(page);
    }

    /**
     * 添加管理员
     */
    @PostMapping("/add")
    public Result add(@RequestBody @Valid AdminSaveDTO adminSaveDTO) {
        adminService.add(adminSaveDTO);
        return Result.success();
    }

    /**
     * 删除管理员
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody List<Long> adminIds) {
        adminService.delete(adminIds);
        return Result.success();
    }

    /**
     * 重置密码
     */
    @GetMapping("/reset")
    public Result reset(@RequestParam Long adminId) {
        adminService.reset(adminId);
        return Result.success();
    }

}
